package com.teamblueplus.websearch;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.teamblueplus.R;

/**
 *This class populates the images from GoogleSearchAPIActivity class
 *and display it in a gridview layout. This class extends the baseadapter
 *library for android.
 * 
 * @author hanzen
 */
public class GridViewImageAdapter extends BaseAdapter {
	private Context context;
	private Activity activity;
	private ArrayList<Object> listImages;
	private static LayoutInflater inflater=null;
	public ImageLoader imageLoader; 

	public GridViewImageAdapter(Activity activity,  ArrayList<Object> listImages) {
		
		this.activity = activity;
		this.listImages = listImages;
		
		
		 inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	     imageLoader=new ImageLoader(activity.getApplicationContext());
	}
	
	 public static class ViewHolder{
	    	public ImageView imgViewImage;
	    	public TextView txtViewTitle;
	    	
	 }


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View vi=convertView;
    	ViewHolder holder;
		if(convertView==null){
			vi = inflater.inflate(R.layout.gridview_row, null);
			holder=new ViewHolder();
			
			holder.imgViewImage=(ImageView)vi.findViewById(R.id.grid_item_image);
			
			vi.setTag(holder);
		}
		else
			holder=(ViewHolder)vi.getTag();
		
		GoogleImageBean imageBean = (GoogleImageBean) listImages.get(position);
		holder.imgViewImage.setTag(imageBean.getThumbUrl());
		imageLoader.DisplayImage(imageBean.getThumbUrl(), activity, holder.imgViewImage);
		
		return vi;
	}

	 public int getCount() {
	        return listImages.size();
	    }

	    public Object getItem(int position) {
	    
	    	return listImages.get(position);
	    }

	    public long getItemId(int position) 
	    {
	    	return position;
	    }
	    
	    
	

}
